package com.ww.core.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ww.core.R;

public class AlertDialog {
	private Context context;
	private Dialog dlg;

	public AlertDialog(Context context) {
		this.context = context;
	}

	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 */
	public void show(String title, String content, OnClickListener okClick) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_alert);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dlg.cancel();
			}
		});
	}
	
	
	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 */
	public void show(String title, String content,int icon, OnClickListener okClick) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_alert);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		ImageView imageView = (ImageView) window.findViewById(R.id.icon);
		imageView.setImageResource(icon);
		imageView.setVisibility(View.VISIBLE);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dlg.cancel();
			}
		});
	}
	
	
	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 */
	public void exitApp() {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_alert);
		TextView title = (TextView) window.findViewById(R.id.title);
		TextView content = (TextView) window.findViewById(R.id.content);
		title.setText(context.getResources().getString(R.string.app_name));
		content.setText(context.getResources().getString(R.string.shureExit));
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
				android.os.Process.killProcess(Process.myPid());
			}
		});
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();
			}
		});
	}
	

	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 * @param cancelClick 取消按钮点击事件.
	 */
	public void show(String title, String content, OnClickListener okClick, OnClickListener cancelClick) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_alert);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(cancelClick);
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}
}
